﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
using DomainModel;
using System.Data.SQLite;

namespace DataAccess
{
    public class AdminAccess : IAdminProvider
    {
        public void CreateAdmin(Admin newManager)
        {
            using (Main db = new Main())
            {
                db.GetTable<Admin>().InsertOnSubmit(newManager);
                db.SubmitChanges();
            }
        }

        public IEnumerable<Admin> GetAllManagers()
        {
            using (Main db = new Main())
            {
                var query = from a in db.GetTable<Admin>()
                            select a;

                return query.AsEnumerable<Admin>();
            }
        }

        public void DeleteAdmin(int id)
        {
            using (Main db = new Main())
            {
                var instance = (from a in db.GetTable<Admin>()
                                where a.ID == id
                                select a).First<Admin>();

                db.GetTable<Admin>().DeleteOnSubmit(instance);
                db.SubmitChanges();
            }
        }

        public void UpdateAdmin(Admin managerToUpdae)
        {
            using (Main db = new Main())
            {
                var query = (from a in db.GetTable<Admin>()
                             where a.ID == managerToUpdae.ID
                             select a).First<Admin>();

                query.AdminName = managerToUpdae.AdminName;
                query.AdminPwd = managerToUpdae.AdminPwd;

                db.SubmitChanges();
            }
        }

        public Admin GetManagerByID(int id)
        {
            using (Main db = new Main())
            {
                return (from a in db.GetTable<Admin>()
                        where a.ID == id
                        select a).First<Admin>();
            }
        }

        public void UpdateSiteConfig(SiteConfig config)
        {
            using (Main db = new Main())
            {
                var query = (from c in db.GetTable<SiteConfig>()
                             select c).First<SiteConfig>();

                query.Address = config.Address;
                query.BackupNumber = config.BackupNumber;
                query.CopRright = config.CopRright;
                query.Description = config.Description;
                query.Email = config.Email;
                query.Fax = config.Fax;
                query.KeyWord = config.KeyWord;
                query.Logo = config.Logo;
                query.PostCode = config.PostCode;
                query.RegisterNumber = config.RegisterNumber;
                query.SiteName = config.SiteName;
                query.Style = config.Style;
                query.Telephone = config.Telephone;
                query.Url = config.Url;

                db.SubmitChanges();
            }
        }

        #region module


        public void CreateModule(Module module)
        {
            using (Main db = new Main())
            {
                db.GetTable<Module>().InsertOnSubmit(module);
                db.SubmitChanges();
            }

        }

        public void DeleteModuleByID(int id)
        {
            using (Main db = new Main())
            {
                var query = this.GetModuleByID(id);
                db.GetTable<Module>().DeleteOnSubmit(query);

                db.SubmitChanges();
            }
        }

        public void UpdateModule(Module moduleToUpdate)
        {
            using (Main db = new Main())
            {
                var query = (from m in db.GetTable<Module>()
                             where m.ID == moduleToUpdate.ID
                             select m).Single<Module>();

                query.Content = moduleToUpdate.Content;
                query.HomePage = moduleToUpdate.HomePage;
                query.IsShow = moduleToUpdate.IsShow;
                query.ModuleName = moduleToUpdate.ModuleName;
                query.Rank = moduleToUpdate.Rank;
                query.Target = moduleToUpdate.Target;

                db.SubmitChanges();
            }
        }

        public Module GetModuleByID(int id)
        {
            using (Main db = new Main())
            {
                return (from m in db.GetTable<Module>()
                        where m.ID == id
                        select m).Single<Module>();
            }
        }

        public IEnumerable<Module> GetAllModules()
        {
            using (Main db = new Main())
            {
                return (from m in db.GetTable<Module>()
                        select m).AsEnumerable<Module>();
            }
        }

        #endregion
    }
}
